var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();

$CertusFormAjax.ajax("findComList", {}, function (response) {
    $$scope.data = response.data;
    $CertusFormAjax.ajax("getPdlines", {}, function (response) {
        $$scope.dd = response.data;
        loadInfo();
    });
});
$CertusFormAjax.ajax("findViewCompany", {}, function (response) {
    var data = response.data;
    if (data.indexOf("STSJT") > -1) {
        $$scope.isStsjt = true;
    }
    if (data.indexOf("STSGD") > -1) {
        $$scope.isGdxx = true;
    }
    if (data.indexOf("HFKJ") > -1) {
        $$scope.isHfkj = true;
    }
    if (data.indexOf("isGdxx") > -1) {
        $$scope.isStsjt = true;
    }
});

//项目其他费用表单显示隐藏
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};

function showtarget() {
    $("#proTarget").val("");
    $("#tarFinishTime").val("");
    $(".tarhideproin").show();
};
tarhideproin();

function tarhideproin() {
    $(".tarhideproin").hide();
};

function formatTime(param) {
    if (param != null && param != undefined && param != "") {
        var date = new Date(param);
        var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
        return content;
    } else {
        return "";
    }
}

var delL = [];
var newMoneyArr = [];

function ckIsN(value) {
    if (value == "N") {
        $("#pdLine").find("input[type='checkbox']").each(function (i, n) {
            if ($(n).val() != "N") {
                $(n).attr("checked", false);
            }
        });
    } else {
        $("#pdLine").find("input[type='checkbox']").each(function (i, n) {
            if ($(n).val() == "N") {
                $(n).attr("checked", false);
            }
        });
    }
}

function loadInfo() {
    $CertusFormAjax.ajax("getProFiles",{},function (response) {
        var data = response.data;
        $$scope.wjList = data;
    });

    $CertusFormAjax.ajax("findLxInfo", {
        "processId": processId
    }, function (result) {
        var data = result.data.map;
        $$scope.proArea = data.pro_area;
        $$scope.proType = data.pro_type;
        $$scope.company = data.comp_id;
        getProduct(data.comp_id);
        $CertusFormAjax.ajax("getNlxRoles",{"company":data.comp_id},function (response) {
            var data = response.data;
            $$scope.rlList = data;
        });
        if (data.pro_type == "XS") {
            $$scope.showCus = true;
            $$scope.cusId = data.cus_id + "";
            $$scope.cusName = data.cus_name;
            $$scope.showArea = true;
            $$scope.showCompare = true;
            $$scope.showSign = true;
            $$scope.showHk = true;
            $$scope.showPolucy = true;
            $$scope.showCompe = true;
            $$scope.showMoney = true;
            $$scope.showBudget = true;
            $$scope.showCtMoney = true;
            $$scope.showFinish = false;
            $$scope.showVersion = false;
            $$scope.divineSignDate = data.divine_sign_date;
            $$scope.divineHkDate = data.divine_hk_date;
        } else if (data.pro_type == "ZL" || data.pro_type == "SW" ||
            data.pro_type == "BM") {
            $$scope.showCus = false;
            $$scope.showArea = false;
            $$scope.showCompare = false;
            $$scope.showSign = false;
            $$scope.showHk = false;
            $$scope.showPolucy = false;
            $$scope.showCompe = false;
            $$scope.showMoney = false;
            $$scope.showBudget = false;
            $$scope.showCtMoney = true;
            $$scope.showFinish = true;
            $$scope.showVersion = false;
            $$scope.divineFinishTime = data.dfTime;

            $("#lxLevel").prop("disabled", "disabled");
            $("#urgency").prop("disabled", "disabled");
            $("#importance").prop("disabled", "disabled");

            $("#datetime-5").prop("disabled", "disabled");
            $("#datetime-6").prop("disabled", "disabled");
        } else {
            $$scope.showCus = false;
            $$scope.showArea = false;
            $$scope.showCompare = false;
            $$scope.showSign = false;
            $$scope.showHk = false;
            $$scope.showPolucy = false;
            $$scope.showCompe = false;
            $$scope.showMoney = false;
            $$scope.showBudget = false;
            $$scope.showVersion = true;
            $$scope.showFinish = false;
            $$scope.divineVersionTime = data.dvTime
        }
        //判断自己是否是项目经理
        if (data.is_mg == "true") {
            $("#remember").prop("checked", "checked");
            $('.hideprobox').show();
        } else {
            // if (result.data.isZdMg=="true"){
            //     $("#remember").prop("checked","checked");
            //     $('.hideprobox').show();
            // }else{
            $("#remember").prop("checked", "");
            $('.hideprobox').hide();
            // }
        }

        $$scope.proName = data.pro_name;
        $$scope.isCompare = data.is_compare + "";

        $$scope.lxLevel = data.pro_lv + "";
        $$scope.urgency = data.urgency + "";
        $$scope.importance = data.importance + "";

        $$scope.clueDesc = data.clue_desc;
        $$scope.cusPolicy = data.cus_policy;
        $$scope.competitor = data.competitor;
        $$scope.lxDesc = data.lx_desc;
        $$scope.startTime2 = data.startTime;
        $$scope.endTime2 = data.endTime;
        if (data.pd_line_codes2!=null){
            var deptCodes = data.pd_line_codes2.split(",");
            for (var i = 0; i < deptCodes.length; i++) {
                $("#pdLine").find("input[type='checkbox']").each(function (k, n) {
                    if ($(n).val() == deptCodes[i]) {
                        $(n).attr("checked", "checked");
                    }
                });
            }
        }

        $$scope.result = data.result;
        var arr = result.data.bugetList;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].bugetType === "预计销售额") {
                $$scope.divineMoney = arr[i].buget;
                $$scope.dmId = arr[i].id;
            } else if (arr[i].bugetType === "客户预算") {
                $$scope.cusBudget = arr[i].buget;
                $$scope.bumId = arr[i].id;
            } else if (arr[i].bugetType === "客户交际费用") {
                $$scope.divineContactMoney = arr[i].buget;
                $$scope.cmId = arr[i].id;
                $$scope.dcMoneyDesc = arr[i].comments;
                $$scope.ctStartTime = formatTime(arr[i].budgetBegin);
                $$scope.ctEndTime = formatTime(arr[i].budgetEnd);
            } else if (arr[i].bugetType === "差旅费用") {
                $$scope.divineBusinessMoney = arr[i].buget;
                $$scope.bmId = arr[i].id;
                $$scope.dbMoneyDesc = arr[i].comments;
                $$scope.bsStartTime = formatTime(arr[i].budgetBegin);
                $$scope.bsEndTime = formatTime(arr[i].budgetEnd);
            } else if (arr[i].bugetType === "采购费用") {
                $$scope.divinePurchaseMoney = arr[i].buget;
                $$scope.pmId = arr[i].id;
                $$scope.dpMoneyDesc = arr[i].comments;
                $$scope.phStartTime = formatTime(arr[i].budgetBegin);
                $$scope.phEndTime = formatTime(arr[i].budgetEnd);
            }
        }

        //其他费用
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].isAdd==1){
                newMoneyArr.push(arr[i]);
            }
        }

        var otherColumns = [{
            id: 'buget',
            title: '预算金额',
            type: 'string',
            width: 150,
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
                var content = '￥' + value + '元';
                return content;
              }
        }, {
            id: 'budgetBegin',
            title: '开始时间',
            type: 'string',
            width: 100,
            columnClass: 'text-center',
            resolution: function (value) {
                var content = '';
                if (value == "") {
                    return content;
                } else {
                    var date = new Date(value);
                    content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                    return content;
                }

            }
        }, {
            id: 'budgetEnd',
            title: '结束时间',
            type: 'string',
            width: 100,
            columnClass: 'text-center',
            resolution: function (value) {
                var content = '';
                if (value == "") {
                    return content;
                } else {
                    var date = new Date(value);
                    content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                    return content;
                }

            }
        }, {
            id: 'comments',
            title: '事由',
            type: 'string',
            columnClass: 'text-left',
        },
        {
            id: 'operation',
            title: '操作',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                content += '<button class="btn btn-xs btn-danger" onclick="deleteWarnA(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
                return content;
            }
        }
        ];
        var otherOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '其他费用',
            datas: newMoneyArr,
            columns: otherColumns,
            gridContainer: 'other1Container',
            tools: ''
        };
        var otherGrid = $.fn.dlshouwen.grid.init(otherOption);
        otherGrid.load();

        //添加其他费用
        var om = -1;
        $(".addMoney").on('click', function () {
            var bugetMoney = $("#bugetMoney").val();
            var comments = $("#comments").val();
            if (bugetMoney == "") {
                layer.msg("预算金额不能为空");
                return;
            }
            if (comments == "") {
                layer.msg("事由不能为空");
                return;
            }
            var arrn = {};
            if ($("#moneyType").length>1){
                arrn.moneyType = $("#moneyType").val();
            }
            arrn.id = om;
            arrn.buget = bugetMoney;
            arrn.comments = comments;

            var budgetBegin = $("#oStartTime").val();
            var budgetEnd = $("#oEndTime").val();
            if (budgetBegin == null || budgetBegin == "" || budgetBegin == undefined) {
                layer.msg('开始日期不能为空!');
                return;
            }
            if (budgetEnd == null || budgetEnd == "" || budgetEnd == undefined) {
                layer.msg('结束日期不能为空!');
                return;
            }
            if (new Date(budgetBegin.replace("/", "-").replace("/", "-")) >
                new Date(budgetEnd.replace("/", "-").replace("/", "-"))) {
                layer.msg('开始日期不能大于结束日期!');
                return;
            }
            arrn.budgetBegin = budgetBegin;
            arrn.budgetEnd = budgetEnd;
            newMoneyArr.push(arrn);
            om--;
            otherGrid.load();
            $('.hideother').hide();

            $("#bugetMoney").val("");
            $("#oStartTime").val("");
            $("#oEndTime").val("");
            $("#comments").val("");
        });
        //删除费用
        this.deleteWarnA = function (wid) {
            layer.confirm('确认删除此数据？', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                var dArr = {};
                for (var i = 0; i < newMoneyArr.length; i++) {
                    if (newMoneyArr[i].id == wid) {
                        dArr = newMoneyArr[i];
                        delL.push(wid);
                    }
                }
                newMoneyArr.splice($.inArray(dArr, newMoneyArr), 1);
                otherGrid.load();
                layer.msg('删除成功');
            });

        }

        $$scope.moneyDesc = data.money_desc;

        $$scope.proId = data.pro_id;
        $$scope.id = data.id;

        var warrList = result.data.warrList;
        var targetList = result.data.targetList;
        loadWarr(warrList);
        loadTarget(targetList);
        loadRt(result.data.rtList);
        loadWj(result.data.wjList);
        loadPdu(result.data.pduList);

        $CertusFormAjax.ajax("findTpmList",{"proId":data.pro_id},function (response) {
            var data = response.data;
            for (var i = 0;i<data.length;i++){
                if (data[i].proRoleCode.indexOf("XMJL")>-1){
                    console.log(data[i])
                    $$scope.proMgId = data[i].userId;
                    $$scope.proMgName = data[i].userName+"("+data[i].userId+")";
                }
                if (data[i].proRoleCode.indexOf("XSJL")>-1){
                    $$scope.proXsId = data[i].userId;
                    $$scope.proXsName = data[i].userName+"("+data[i].userId+")";
                }
                if (data[i].proRoleCode.indexOf("CPJL")>-1){
                    $$scope.proCpId = data[i].userId;
                    $$scope.proCpName = data[i].userName+"("+data[i].userId+")";
                }
                if (data[i].proRoleCode.indexOf("JFJL")>-1){
                    $$scope.proJfId = data[i].userId;
                    $$scope.proJfName = data[i].userName+"("+data[i].userId+")";
                }
            };
        });
    });
}

this.getProduct = function (companyCode) {
    if (companyCode == undefined || companyCode == null || companyCode == "") {
        companyCode = $("#company").val();
    }else{
        if (companyCode=="STSNJ"||companyCode=="STSBJ"||
            companyCode=="STSSH"||companyCode=="STSGD"){
            companyCode = "STSJT";
        }
    }
    $CertusFormAjax.ajax("findIntroList", {
        "compCode": companyCode
    }, function (response) {
        $$scope.productList = response.data;
    });
};

var warnData = [];
//加载项目指标
function loadWarr(warrList) {
    if (warrList != "") {
        warnData = warrList;
    }
    var proinColumns = [{
        id: 'wntyName',
        title: '指标名称',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'wntyDesc',
        title: '指标描述',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'finishTime',
        title: '完成时间',
        type: 'string',
        width: 100,
        columnClass: 'text-left',
        resolution: function (value) {
            var content = '';
            if (value == "") {
                return content;
            } else {
                var date = new Date(value);
                content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                return content;
            }
        }
    },
    {
        id: 'operation',
        title: '操作',
        width: 60,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            // content += '<button class="btn btn-xs btn-info" onclick="editWarn('+record.id+');"><i class="fa fa-edit"></i> 编辑</button>';
            // content+=' ';
            content += '<button class="btn btn-xs btn-danger" onclick="deleteWarn(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }
    ];
    $("#proin1Container").empty();
    var proinOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '项目指标',
        datas: warnData,
        columns: proinColumns,
        gridContainer: 'proin1Container',
        tools: ''
    };
    var proinGrid = $.fn.dlshouwen.grid.init(proinOption);
    proinGrid.load()
}
var targetData = [];

function loadTarget(targetList) {
    if (targetList != "") {
        targetData = targetList;
    }
    // 立项信息-项目目标
    var prointColumns = [{
        id: 'proTarget',
        title: '项目目标',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'finishTime',
        title: '完成时间',
        type: 'string',
        width: 130,
        columnClass: 'text-center',
        resolution: function (value) {
            var content = '';
            if (value == "") {
                return content;
            } else {
                var date = new Date(value);
                content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                return content;
            }
        }
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        width: 75,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-danger" onclick="deleteTarget(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }
    ];
    $("#targetContainer").empty();
    //项目目标列表
    var prointOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '项目目标',
        datas: targetData,
        columns: prointColumns,
        gridContainer: 'targetContainer',
        tools: ''
    };
    var prointGrid = $.fn.dlshouwen.grid.init(prointOption);
    prointGrid.load();
}


var tarid = 0;
$(".targetSubmit").on('click', function () {
    var proTarget = $("#proTarget").val();
    var finishTime = $("#tarFinishTime").val();
    if (proTarget == "") {
        layer.msg("项目目标不能为空");
        return;
    }
    if (proTarget.length > 120) {
        layer.msg("项目目标不能大于120字");
        return;
    }
    if (finishTime == "") {
        layer.msg("完成时间不能为空");
        return;
    }
    var targetArr = {};
    targetArr.proTarget = proTarget;
    targetArr.finishTime = finishTime;
    targetArr.id = tarid;
    targetData.push(targetArr);
    tarid--;
    loadTarget("");
    $(".tarhideproin").hide();
});

//从负数开始，防止ID重复
var warnI = 0;
$(".warnSubmit").on('click', function () {
    var wntyName = $("#wntyName").val();
    var wntyDesc = $("#wntyDesc").val();
    var finishTime = $("#finishTime").val();
    if (wntyName == "") {
        layer.msg("指标名称不能为空");
        return;
    }
    if (wntyDesc == "") {
        layer.msg("指标描述不能为空");
        return;
    }
    if (finishTime == "") {
        layer.msg("完成时间不能为空");
        return;
    }
    var warnArr = {};
    var wid = $("#warrId").val();
    //表示新增
    if (wid == 0) {
        warnArr.wntyName = wntyName;
        warnArr.wntyDesc = wntyDesc;
        warnArr.finishTime = finishTime;
        warnArr.id = warnI;
        warnData.push(warnArr);
        warnI--;
    } else {
        //编辑
        for (var i = 0; i < warnData.length; i++) {
            if (warnData[i].id == wid) {
                warnData[i].wntyName = wntyName;
                warnData[i].wntyDesc = wntyDesc;
                warnData[i].finishTime = finishTime;
            }
        }
    }
    $("#wntyName").val("");
    $("#wntyDesc").val("");
    $("#finishTime").val("");
    $("#warrId").val(0);
    loadWarr("");
    $(".hideproin").hide();
});

function deleteWarn(wid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < warnData.length; i++) {
            if (warnData[i].id == wid) {
                dArr = warnData[i];
            }
        }
        warnData.splice($.inArray(dArr, warnData), 1);
        loadWarr(warnData);
        loadTarget("");
        layer.msg('删除成功');
    });
}

//编辑项目指标
function editWarn(wid) {
    showproin();
    for (var i = 0; i < warnData.length; i++) {
        if (warnData[i].id == wid) {
            $("#warrId").val(wid);
            $("#wntyName").val(warnData[i].wntyName);
            $("#wntyDesc").val(warnData[i].wntyDesc);
            $("#finishTime").val(warnData[i].finishTime);
        }
    }
}

var delData = [];

function deleteTarget(wid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < targetData.length; i++) {
            if (targetData[i].id == wid) {
                dArr = targetData[i];
                delData.push(dArr);
            }
        }
        targetData.splice($.inArray(dArr, targetData), 1);
        loadTarget("");
        layer.msg('删除成功');
    });

}

function showproin() {
    $("#wntyName").val("");
    $("#wntyDesc").val("");
    $("#finishTime").val("");
    $(".hideproin").show();
};
//项目指标表单显示隐藏
function hideproin() {
    $(".hideproin").hide();
};
// 立项选择展示隐藏效果
$('#remember').change(function () {
    var status = $('#remember').prop("checked");
    if (status) {
        $('.hideprobox').show();
        $(".tarhideprobox").show();
    } else {
        $('.hideprobox').hide();
        $(".tarhideprobox").hide();
    }
});

function showRl() {
    $(".hiderl").show();
}
function showWj() {
    $(".wjhider").show();
};

var rlData = [];
function loadRt(rtList) {
    if (rtList != "") {
        rlData = rtList;
    }
    var rlColumns = [{
        id: 'roleName',
        title: '角色',
        type: 'string',
        columnClass: 'text-center'
    },{
        id: 'rtMoney',
        title: '成本',
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            if (value=="1"){
                content = "低";
            }else if (value=="2"){
                content = "中";
            }else{
                content = "高";
            }
            return content;
        }
    },{
        id: 'beginTime',
        title: '开始时间',
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value) {
            var date = new Date(value);
            var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
            return content;
        }
    }, {
        id: 'endTime',
        title: '结束时间',
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value) {
            var date = new Date(value);
            var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
            return content;
        }
    },{
        id: 'rtNum',
        title: '天数',
        type: 'string',
        columnClass: 'text-center',
    },{
        id: 'pNum',
        title: '人数',
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'comments',
        title: '说明',
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'operation',
        title: '操作',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-danger" onclick="deleteRl(\'' + record.id + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }];

    $("#rlContainer").empty();
    $("#rlToolBarContainer").empty();
    var rlOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '人力成本列表',
        datas: rlData,
        columns: rlColumns,
        gridContainer: 'rlContainer',
        toolbarContainer: 'rlToolBarContainer',
        tools: ''
    };
    var rlGrid = $.fn.dlshouwen.grid.init(rlOption);
    rlGrid.load();
}

var rid = 1;
$(".rlSubmit").click(function () {
    var rArr = {};
    var userName = $('#rluserName option:selected').text();
    var userId = $("#rluserName").val();
    if (userName==""||userId==""){
        layer.msg("请选择角色！");
        return;
    }
    var rtMoney = $("#rtMoney").val();
    var rlBeginTime = $("#rlBeginTime").val();
    var rlEndTime = $("#rlEndTime").val();
    if (rlBeginTime==""||rlEndTime==""){
        layer.msg("请输入使用周期！");
        return;
    }
    var xzRt = daysBetween(rlBeginTime,rlEndTime)+1;
    var rtNum = $("#rtNum").val();
    if (rtNum==""||rtNum==0){
        layer.msg("请输入天数！");
        return;
    }
    if (rtNum>xzRt){
        layer.msg("天数超出周期时间！");
        return;
    };
    var pNum = $("#pNum").val();
    if (pNum==""||pNum==0){
        layer.msg("请输入人数！");
        return;
    }
    var rlComments = $("#rlComments").val();
    rArr.roleName = userName;
    rArr.roleId = userId;
    rArr.rtMoney = rtMoney;
    rArr.beginTime = rlBeginTime;
    rArr.endTime = rlEndTime;
    rArr.comments = rlComments;
    rArr.pNum = pNum;
    rArr.rtNum = rtNum;
    rArr.id = rid;
    //判断成员是否已经存在
    for(var i=0;i<rlData.length;i++){
        if (rlData[i].roleId==userId){
            layer.msg("成员已经存在！");
            return;
        }
    }
    rid++;
    rlData.push(rArr);
    $(".hiderl").hide();
    cleanRl();
    loadRt("");
});

var delRl = [];
//删除人力
function deleteRl(rid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < rlData.length; i++) {
            if (rlData[i].id == rid) {
                dArr = rlData[i];
                delRl.push(dArr);
            }
        }
        rlData.splice($.inArray(dArr, rlData), 1);
        layer.msg('删除成功');
        loadRt("");
    });
}

var wjData = [];
function loadWj(wjList) {
    if (wjList != "") {
        wjData = wjList;
    }
    //涉及文件
    var wjColumns = [{
        id: 'productName',
        title: '文件名称',
        type: 'string',
        columnClass: 'text-center'
    }, {
        id: 'comments',
        title: '说明',
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'operation',
        title: '操作',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = '';
            content += '<button class="btn btn-xs btn-danger" onclick="deleteWj(\'' + record.productCode + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }];

    var wjOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '人力成本列表',
        datas: wjData,
        columns: wjColumns,
        gridContainer: 'wjContainer',
        toolbarContainer: 'wjToolBarContainer',
        tools: ''
    };
    var wjGrid = $.fn.dlshouwen.grid.init(wjOption);
    wjGrid.load();
}
var pwid = 1;
$(".wjSubmit").click(function () {
    // var wjId = $("#wjName").val();
    // if (wjId==""){
    //     layer.msg("请选择文件！");
    //     return;
    // }
    // var wjName = $('#wjName option:selected').text();
    var wjName = $("#wjName").val();
    var comments = $("#wjComments").val();
    var arr = {};
    arr.id = pwid;
    // arr.productCode = wjId;
    arr.productCode = "";
    arr.productName = wjName;
    arr.comments = comments;
    for(var i=0;i<wjData.length;i++){
        if (wjData[i].productName==wjName){
            layer.msg("文件已经存在！");
            return;
        }
    }
    pwid++;
    wjData.push(arr);
    $(".wjhider").hide();
    loadWj("");
    cleanWj();
});
var delWj = [];
function deleteWj(pwid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < wjData.length; i++) {
            if (wjData[i].productCode == pwid) {
                dArr = wjData[i];
                delWj.push(dArr);
            }
        }
        wjData.splice($.inArray(dArr, wjData), 1);
        wjGrid.load();
        layer.msg('删除成功');
        loadWj("");
    });
}

//添加产品
function loadPdu(pduList) {
    var isNlx = false;
    //判断是否是新立项流程
    for (var i=0;i<pduList.length;i++){
        //如果有记录为1，则为新立项流程
        if (pduList[i].isNLx == 1){
            isNlx = true;
            break;
        }
    }
    if (isNlx){
        setTimeout(function () {
            var arr = pduList[0];
            $("#product").val(arr.productId);
        },100);
    }else{
        //加载多个产品信息
        var cpColumns = [{
            id: 'productName',
            title: '文件名称',
            type: 'string',
            columnClass: 'text-center'
        }, {
            id: 'comments',
            title: '说明',
            type: 'string',
            columnClass: 'text-center',
        }, {
            id: 'operation',
            title: '操作',
            width: 75,
            type: 'string',
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                content += '<button class="btn btn-xs btn-danger" onclick="deleteCp(\'' + record.productId + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
                return content;
            }
        }];

        var cpOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '人力成本列表',
            datas: pduList,
            columns: cpColumns,
            gridContainer: 'cpContainer',
            toolbarContainer: 'cpToolBarContainer',
            tools: ''
        };
        var cpGrid = $.fn.dlshouwen.grid.init(cpOption);
        cpGrid.load();
        //-------------end
    }
}

function cleanRl() {
    $("#rluserId").val("");
    $("#rlBeginTime").val("");
    $("#rlEndTime").val("");
    $("#rlComments").val();
}
//添加人力成本
function hideRl() {
    $(".hiderl").hide();
    cleanRl();
}

$(".submit").on('click', function () {
    if (!certusValidateNotnull()) {
        return;
    }
    $CertusFormAjax.ajax("isHasProName", {
        "proName": $("#proName").val()
    }, function (res) {
        if (res.data == true) {
            layer.msg("项目名称已存在！", {
                "time": 1000
            });
        } else {
            var sTime = $("#datetime-5").val();
            var endTime = $("#datetime-6").val();
            if (endTime == "") {
                layer.msg("结束日期不能为空", {
                    time: 1000
                });
                return;
            }
            if (sTime != "") {
                var startTime = new Date(sTime.replace("/", "-").replace("/", "-"));
                var endTimes = new Date(endTime.replace("/", "-").replace("/", "-"));
                if (startTime > endTimes) {
                    layer.msg("开始日期不能大于结束日期", {
                        time: 1000
                    });
                    return;
                }
            } else {
                layer.msg("开始日期不能为空", {
                    time: 1000
                });
                return;
            }

            //客户交际费用
            var ctStartTime = $("#ctStartTime").val();
            var ctEndTime = $("#ctEndTime").val();
            if (new Date(ctStartTime.replace("/", "-").replace("/", "-"))
                > new Date(ctEndTime.replace("/", "-").replace("/", "-"))) {
                layer.msg("客户交际费用开始时间不能大于结束时间", { time: 1000 });
                return;
            }

            //差旅费用
            var bsStartTime = $("#bsStartTime").val();
            var bsEndTime = $("#bsEndTime").val();
            if (new Date(bsStartTime.replace("/", "-").replace("/", "-"))
                > new Date(bsEndTime.replace("/", "-").replace("/", "-"))) {
                layer.msg("差旅费用开始时间不能大于结束时间", { time: 1000 });
                return;
            }

            //采购费用
            var phStartTime = $("#phStartTime").val();
            var phEndTime = $("#phEndTime").val();
            if (new Date(phStartTime.replace("/", "-").replace("/", "-"))
                > new Date(phEndTime.replace("/", "-").replace("/", "-"))) {
                layer.msg("采购费用开始时间不能大于结束时间", { time: 1000 });
                return;
            }
            var pdLineCodes = "";
            if (!$("#pdLine").is(":hidden")) {
                $("#pdLine").find("input[type='checkbox']:checked").each(function (i, n) {
                    if (i == 0) {
                        pdLineCodes += $(n).val();
                    } else {
                        pdLineCodes += "," + $(n).val();
                    }
                });
                if (pdLineCodes == "") {
                    layer.msg("至少选择一个产品线", {
                        time: 1000
                    });
                    return;
                }
            }
            var arr = $("form").serializeArray();
            var data = {};
            for (var i = 0; i < arr.length; i++) {
                data[arr[i].name] = arr[i].value;
            };
            var isCheck = document.getElementById("remember").checked;
            data.isCheck = isCheck;
            data.stepId = stepId;
            data.warnData = warnData;
            data.newMoney = newMoneyArr;
            data.targetData = targetData;
            data.delData = delData;
            data.delMoney = delL;
            data.proType = $$scope.proType;
            data.pdLineCodes2 = pdLineCodes;
            data.startTime2 = sTime;
            data.endTime2 = endTime;
            data.isMg = $('#remember').prop("checked");

            data.lxLevel = $("#lxLevel").val();
            data.urgency = $("#urgency").val();
            data.importance = $("#importance").val();
            data.rlData = rlData;
            data.delRl = delRl;
            // 开启
            var index = layer.load(1);
            $CertusFormAjax.ajax("updateLxSq", {
                "data": JSON.stringify(data)
            }, function () {
                //关闭
                layer.close(index);
                layer.msg("申请成功");
                window.history.back();
            });
        }
    });
});

$(".submit1").click(function () {
    if (!certusValidateNotnull()) {
        return;
    }
    $CertusFormAjax.ajax("isHasProName", {
        "proName": $("#proName").val()
    }, function (res) {
        if (res.data == true) {
            layer.msg("项目名称已存在！", {
                "time": 1000
            });
        } else {
            var ckProMgUser = $("#proMgName").val();
            var ckProMgId = $("#proMgId").val();
            if (ckProMgUser.indexOf(ckProMgId)==-1){
                layer.msg("请选择正确的格式的项目经理");
                return;
            }
            var ckProXsUser = $("#proXsName").val();
            var ckProXsId = $("#proXsId").val();
            if(ckProXsId!=undefined){
                if (ckProXsUser.indexOf(ckProXsId)==-1){
                    layer.msg("请选择正确的格式的销售经理");
                    return;
                }
            }
            var ckProCpName = $("#proCpName").val();
            var ckProCpId = $("#proCpId").val();
            if (ckProCpId!=undefined){
                if (ckProCpName.indexOf(ckProCpId)==-1){
                    layer.msg("请选择正确的格式的产品经理");
                    return;
                }
            }
            var ckProJfName = $("#proJfName").val();
            var ckProJfId = $("#proJfId").val();
            if (ckProJfId!=undefined){
                if (ckProJfName.indexOf(ckProJfId)==-1){
                    layer.msg("请选择正确的格式的交付经理");
                    return;
                }
            }

            var sTime = $("#datetime-5").val();
            var endTime = $("#datetime-6").val();
            if (endTime == "") {
                layer.msg("结束日期不能为空", {
                    time: 1000
                });
                return;
            }
            if (sTime != "") {
                var startTime = new Date(sTime.replace("/", "-").replace("/", "-"));
                var endTimes = new Date(endTime.replace("/", "-").replace("/", "-"));
                if (startTime > endTimes) {
                    layer.msg("开始日期不能大于结束日期", {
                        time: 1000
                    });
                    return;
                }
            } else {
                layer.msg("开始日期不能为空", {
                    time: 1000
                });
                return;
            }

            if ($("#product").val()==""){
                layer.msg("请选择所属产品！");
                return;
            }

            var arr = $("form").serializeArray();
            var data = {};
            for (var i = 0; i < arr.length; i++) {
                data[arr[i].name] = arr[i].value;
            };
            data.stepId = stepId;
            data.warnData = warnData;
            data.newMoney = newMoneyArr;
            data.targetData = targetData;
            data.delData = delData;
            data.delMoney = delL;
            data.proType = "XS";
            data.pdLineCodes2 = "";
            data.startTime2 = sTime;
            data.endTime2 = endTime;

            data.lxLevel = $("#lxLevel").val();
            data.urgency = $("#urgency").val();
            data.importance = $("#importance").val();
            data.rlData = rlData;
            data.delRl = delRl;
            data.wjData = wjData;
            data.delWj = delWj;
            var pduList = [];
            var pduArr = {};
            pduArr.productId = $("#product").val();
            pduArr.productName = $('#product option:selected').text();
            pduList.push(pduArr);
            data.pduData = pduList;
            // 开启
            var index = layer.load(1);
            $CertusFormAjax.ajax("toEditNlx", {
                "data": JSON.stringify(data)
            }, function () {
                //关闭
                layer.close(index);
                layer.msg("申请成功");
                window.history.back();
            });
        }
    });
});

function daysBetween(sDate1,sDate2){
    //Date.parse() 解析一个日期时间字符串，并返回1970/1/1 午夜距离该日期时间的毫秒数
    var time1 = Date.parse(new Date(sDate1));
    var time2 = Date.parse(new Date(sDate2));
    var nDays = Math.abs(parseInt((time2 - time1)/1000/3600/24));
    return  nDays;
};